/******************************************************************************* 
 * Copyright (c) 2008 xored software, Inc.  
 * 
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html  
 * 
 * Contributors: 
 *     xored software, Inc. - initial API and Implementation (Yuri Strot) 
 *******************************************************************************/
package com.xored.glance.internal.ui.preferences;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.ColorFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.xored.glance.internal.ui.GlancePlugin;
import com.xored.glance.ui.sources.ColorManager;

/**
 * @author Yuri Strot
 * 
 */
public class GlancePreferencePage extends FieldEditorPreferencePage implements
		IWorkbenchPreferencePage, IPreferenceConstants {

	public GlancePreferencePage() {
		super(GRID);
	}

	public void init(IWorkbench workbench) {
	}

	@Override
	protected IPreferenceStore doGetPreferenceStore() {
		return GlancePlugin.getDefault().getPreferenceStore();
	}

	@Override
	protected void createFieldEditors() {
		createSearchSettings(getFieldEditorParent());
		createPanelSettings(getFieldEditorParent());
		createColorSettings(getFieldEditorParent());
	}

	/**
	 * Adjust the layout of the field editors so that they are properly aligned.
	 */
	protected void adjustGridLayout() {
		super.adjustGridLayout();
		((GridLayout) getFieldEditorParent().getLayout()).numColumns = 1;
	}

	private Group createPanelSettings(Composite parent) {
		Group group = new Group(parent, SWT.NONE);
		group.setText("Panel");
		group.setLayout(new GridLayout(1, false));
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		Composite composite = new Composite(group, SWT.NONE);
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		addField(new BooleanFieldEditor(PANEL_STATUS_LINE,
				"Show panel in status line when possible", composite));
		addField(new BooleanFieldEditor(PANEL_DIRECTIONS,
				"Show direction buttons", composite));
		addField(new BooleanFieldEditor(PANEL_CLOSE, "Show close button",
				composite));
		addField(new IntegerFieldEditor(PANEL_TEXT_SIZE,
				"Default box width in chars:", composite));

		return group;
	}

	private Group createSearchSettings(Composite parent) {
		Group group = new Group(parent, SWT.NONE);
		group.setText("Search");
		group.setLayout(new GridLayout(1, false));
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		Composite composite = new Composite(group, SWT.NONE);
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		addField(new BooleanFieldEditor(SEARCH_CASE_SENSITIVE,
				LABEL_CASE_SENSITIVE, composite));
		addField(new BooleanFieldEditor(SEARCH_CAMEL_CASE, LABEL_CAMEL_CASE,
				composite));
		addField(new BooleanFieldEditor(SEARCH_WORD_PREFIX, LABEL_WORD_PREFIX,
				composite));
		addField(new BooleanFieldEditor(SEARCH_REGEXP, LABEL_REGEXP, composite));

		return group;
	}

	private Group createColorSettings(Composite parent) {
		Group group = new Group(parent, SWT.NONE);
		group.setText("Color");
		group.setLayout(new GridLayout(1, false));
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		Composite composite = new Composite(group, SWT.NONE);
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		addField(new ColorEditor(composite));

		return group;
	}

	private static class ColorEditor extends ColorFieldEditor {

		public ColorEditor(Composite parent) {
			super(ColorManager.COLOR_BACKGROUND, "Background:", parent);
			super.setPreferenceStore(ColorManager.getStore());
		}

		@Override
		public void setPreferenceStore(IPreferenceStore store) {
		}

	}

}
